import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Endstart
 * Date: 2021-05-07
 * Time: 21:23
 */
public class SocialNetwork {


    public static void main(String[] args) {
        test(new String[]{"NNN", "NNN", "NNN"});
        test(new String[]{"NYY", "YNY", "YYN"});
        test(new String[]{"NYNNN", "YNYNN", "NYNYN", "NNYNY", "NNNYN"});
    }

    public static void test(String[] friends) {
        int maxConnected = mostConnected(friends);
        System.out.println(Arrays.toString(friends));
        System.out.println(maxConnected);
        System.out.println();
    }

    public static int mostConnected(String[] friends) {
        if (friends == null || friends.length == 0) {
            return 0;
        }

        int max = 0;
        for (int i = 0; i < friends.length; i++) {
            Set<String> friendLinks = new HashSet<>();

            Stack<Integer> stack = new Stack<>();
            boolean[] visits = new boolean[friends.length];
            stack.add(i);
            while (!stack.isEmpty()) {
                Integer friendId = stack.pop();
                if (visits[friendId]) {
                    continue;
                }
                visits[friendId] = true;

                String friend = friends[friendId];
                for (int j = 0; j < friend.length(); j++) {
                    if (j == i) {
                        continue;
                    }

                    if (friend.charAt(j) == 'Y') {
                        friendLinks.add(i + "-" + j);
                        if (friendId == i) {
                            stack.add(j);
                        }
                    }
                }
            }

            if (max < friendLinks.size()) {
                max = friendLinks.size();
            }
        }

        return max;
    }
}
